package com.naizhuai.ui.presenter;

import com.naizhuai.R;
import com.naizhuai.ui.entity.LabelBean;
import com.naizhuai.util.autocomplete.AutocompletePresenter;
import com.naizhuai.util.autocomplete.RecyclerViewPresenter;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;


public class SlideLabelsPresenter extends RecyclerViewPresenter<LabelBean> {

    protected Adapter adapter;
    private Context mContext;
    private RelativeLayout relative;
    private ViewGroup vgContent;
    private View view;

    public SlideLabelsPresenter(Context context) {
        super(context);
        mContext=context;
    }

    @Override
    protected AutocompletePresenter.PopupDimensions getPopupDimensions() {
        PopupDimensions dims = new PopupDimensions();
        dims.width = 600;
        dims.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        return dims;
    }

    @Override
    protected RecyclerView.Adapter instantiateAdapter() {
        adapter = new Adapter();
        return adapter;
    }

    @Override
    protected ViewGroup getView() {
        relative = new RelativeLayout(mContext);
        vgContent=super.getView();
        relative.addView(vgContent);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        view=inflater.inflate(R.layout.dialog_loading,null);
        RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(600, RelativeLayout.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        relative.addView(view);
        return relative;
    }

    private void initViewVisibility(){
        vgContent.setVisibility(View.INVISIBLE);
        view.setVisibility(View.VISIBLE);
    }

    private void cancelSearchVisibility(){
        view.setVisibility(View.INVISIBLE);
        vgContent.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onQuery(@Nullable CharSequence query) {
        initViewVisibility();
        if(query!=null&&query.length()<2){
            cancelSearchVisibility();
            return;
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                List<LabelBean> all = LabelBean.LABEL_ALL;
                if (TextUtils.isEmpty(query)) {
                    adapter.setData(all);
                } else {
                    List<LabelBean> list = new ArrayList<>();
                    for (LabelBean label : all) {
                        if (label.getName().toLowerCase().contains(query) ||
                                label.getName().toLowerCase().contains(query)) {
                            list.add(label);
                        }
                    }
                    adapter.setData(list);
                    Log.e("UserPresenter", "found "+list.size()+" users for query "+query);
                }
                cancelSearchVisibility();
                adapter.notifyDataSetChanged();
            }
        },1000);

    }

    @Override
    public CharSequence getSubmitMessage() {
        return "";
    }

    class Adapter extends RecyclerView.Adapter<Adapter.Holder> {

        private List<LabelBean> data;

        public class Holder extends RecyclerView.ViewHolder {
            private View root;
            private TextView labelName;
            public Holder(View itemView) {
                super(itemView);
                root = itemView;
                labelName = ((TextView) itemView.findViewById(R.id.label_name));
            }
        }

        public void setData(List<LabelBean> data) {
            this.data = data;
        }

        @Override
        public int getItemCount() {
            return (isEmpty()) ? 1 : data.size();
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Holder(LayoutInflater.from(getContext()).inflate(R.layout.adapter_slide_edit_lables_acomplete, parent, false));
        }

        private boolean isEmpty() {
            return data == null || data.isEmpty();
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            if (isEmpty()) {
                holder.labelName.setText("No user here! Sorry!");
                holder.root.setOnClickListener(null);
                return;
            }
            final LabelBean label = data.get(position);
            holder.labelName.setText("@" + label.getName());
            holder.root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dispatchClick(label);
                }
            });
        }
    }
}
